package com.wr.springbootadmin.common;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.wr.springbootadmin.customizeExceptions.CustomizeException;
import lombok.Data;

import java.io.Serializable;

/**
 * @author 王瑞
 * @description: 接口返回数据的总pojo对象 所有接口都必须返回这个对象
 * @date 2020/12/24  15:35
 */

@Data
@JsonInclude(JsonInclude.Include.NON_NULL)  //如何字段是空的 就不参与序列化
public class ResponseResult implements Serializable {

    private Integer code;

    private String msg;

    private Object data;

    /*私有化所有的构造器*/
    private ResponseResult(){}

    private ResponseResult(Integer code, Object data) {
        this.code = code;
        this.data = data;
    }

    private ResponseResult(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }
    private ResponseResult(Integer code, String msg, Object data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }



    /*成功时返回*/
    public static ResponseResult success(Object data){
        return new ResponseResult(ResultCode.SUCCESS.getCode(), data);
    }

    /*成功时返回*/
    public static ResponseResult success(){
        return new ResponseResult(ResultCode.SUCCESS.getCode(),ResultCode.SUCCESS.getMessage());
    }

    /*失败时返回*/
    public static ResponseResult failed(Integer code, String msg){
        return new ResponseResult(code, msg);
    }

    /*失败时返回  ResultCode自定义枚举对象*/
    public static ResponseResult failed(ResultCode resultCode){
        return new ResponseResult(resultCode.getCode(), resultCode.getMessage());
    }

    /*失败时返回 CustomizeException自定义异常对象 */
    public static ResponseResult failed(CustomizeException customizeException){
        return new ResponseResult(customizeException.getCode(), customizeException.getMessage());
    }
}
